<?php
session_start();
if(isset($_SESSION['admin_id'])) {
include "../include/dbFunctions.php";
$id = $_POST['id'];
//Retrieve data from POST
$education = $_POST['education'];
$program_name = htmlspecialchars($_POST['program_name'],ENT_QUOTES);
$program_description = nl2br(htmlspecialchars($_POST['program_description'],ENT_QUOTES));
$program_lesson_duration = htmlspecialchars($_POST['program_lesson_duration'],ENT_QUOTES);
$program_support = htmlspecialchars($_POST['program_support'],ENT_QUOTES);
$program_status = ($_POST['program_status']);

//update volunteer table
$query1 = "UPDATE program
            SET program_name = '".$program_name."',
                program_description = '".$program_description."',
                program_lesson_duration = '".$program_lesson_duration."',
                program_support = '".$program_support."',
                program_status = '".$program_status."'             
            WHERE program_id=$id";
$result1 = executeQuery($query1);
//update education_preference table
//lazy way - MUST CHANGE!!! lyedit
$query2 = "DELETE FROM program_education WHERE program_id=$id";
$result2 = executeQuery($query2);
for ($i=0;$i<count($education);$i++){
    $education_id = $education[$i];
    $query3 = "INSERT INTO program_education (program_id,education_id) VALUES ($id,$education_id)";
    $result3 = executeQuery($query3);
}

if ($result1!=0){
    $_SESSION['mes']="The record has been updated successfully";
} else {
    $_SESSION['mes']="Error! You cannot edit this program.";
}
$link = "programView.php?id=$id";
header("Location: $link ");
/*
 * echo $id."<br/>";
echo $salutation."<br/>";
echo $name."<br/>";
echo $gender."<br/>";
echo $email."<br/>";
echo $office."<br/>";
echo $mobile."<br/>";
echo $address."<br/>";
echo $sponsor."<br/>";
print_r($education);
echo"<br/>";
echo $day_p."<br/>";
*/

}else {
    header("Location: ../include/login.html");
}
?>